var handleAdd = function() {
	var node = Ext.getCmp("tree").getSelectionModel().getSelection();
	if (node) {
		showAddWindow(node);
	}
}

var handleRemove = function() {
	var node = Ext.getCmp("tree").getSelectionModel().getSelection();
	if (node) {
		Ext.MessageBox.confirm("Confirmation",
				"Are you sure you want to remove " + node[0].raw.text + "?",
				function(btn) {
					if (btn === "yes") {
						performRemoval(node);
						var details = Ext.getCmp("details");
						if (details){
							details.removeAll();
						}
					}
				});
	}
}

var cm = Ext.create("Ext.menu.Menu", {
	items : [ {
		id : "itemAdd",
		handler : handleAdd
	}, {
		id : "itemRemove",
		handler : handleRemove
	} ]
});

var handleOnContextMenuEvent = function(view, rec, node, index, event) {
	event.stopEvent();

	var id = node.viewRecordId;

	var addItem = cm.getComponent("itemAdd");
	var removeItem = cm.getComponent("itemRemove");

	if (/*node.id == "root"*/index == 0) {//we're on the root element
		addItem.setText("Add new company");
		addItem.enable();
		removeItem.setText("Remove");
		removeItem.disable();
	} else if (id.indexOf("company") != -1) {
		addItem.setText("Add new division");
		addItem.enable();
		removeItem.setText("Remove company");
		removeItem.enable();
	} else if (id.indexOf("division") != -1) {
		addItem.setText("Add new project");
		addItem.enable();
		removeItem.setText("Remove division");
		removeItem.enable();
	} else if (id.indexOf("project") != -1) {
		addItem.setText("Add new employee");
		addItem.enable();
		removeItem.setText("Remove project");
		removeItem.enable();
	} else if (id.indexOf("employee") != -1) {
		addItem.setText("Add");
		addItem.disable();
		removeItem.setText("Fire employee");
		removeItem.enable();
	}
	cm.showAt(event.getXY());
}

var handleOnClickEvent = function(view, rec, node, index, event) {
	if (index != 0) {
		showDetails(node.viewRecordId);
	}
}